﻿using CCWin;
using ERPWinFormWork.Product.Model;
using ERPWinFormWork.Product.ProductErrorCode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static ERPWinFormWork.Product.StockinAdd;

namespace ERPWinFormWork.Product
{
    public partial class StockoutAdd : Form
    {
        private ApiClient _apiClient;

        public StockoutAdd()
        {
            InitializeComponent();
            this._apiClient = _apiClient;
            _apiClient = new ApiClient(AppConfig.ApiBaseUrl);
            GetProductSelect1();
            GetProductSelect2();
            GetProductSelect3();
            GetProductSelect4();
            GetProductSelect5();
            GetProductSelect6();
            GetProductSelect7();
        }

        private void skinLabel13_Click(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 库位查询
        /// </summary>
        private async void GetProductSelect1()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Area_Model>>>(
                    endpoint: "Product/AreaShow"
                );
            if (response != null)
            {
                cang2.DataSource = response.data;
                cang2.DisplayMember = "warehouse_area";//指定显示的字段
                cang2.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 库区查询
        /// </summary>
        private async void GetProductSelect2()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Storage_Model>>>(
                    endpoint: "Product/StorageShow"
                );
            if (response != null)
            {
                cang3.DataSource = response.data;
                cang3.DisplayMember = "storage_location";//指定显示的字段
                cang3.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 仓库查询
        /// </summary>
        private async void GetProductSelect3()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Warehouse_Model>>>(
                    endpoint: "Product/WareHouseShow"
                );
            if (response != null)
            {
                cang1.DataSource = response.data;
                cang1.DisplayMember = "warehouse_Name";//指定显示的字段
                cang1.ValueMember = "id";//绑定id属性
            }
        }


        /// <summary>
        /// 产品查询
        /// </summary>
        private async void GetProductSelect4()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Product_Model>>>(
                    endpoint: "Product/ShowProduct"
                );
            if (response != null)
            {
                productname.DataSource = response.data;
                productname.DisplayMember = "product_Name";//指定显示的字段
                productname.ValueMember = "id";//绑定id属性
            }
        }


        /// <summary>
        /// 产品名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void productname_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedProduct = productname.SelectedItem as Product_Model;
            if (selectedProduct != null)
            {
                productnumber.Text = selectedProduct.Product_Number ?? "";
                productgui.Text = selectedProduct.Specification ?? "";

            }
            else
            {
                productnumber.Text = "";
                productgui.Text = "";
            }
        }



        /// <summary>
        /// 客户查询
        /// </summary>
        private async void GetProductSelect5()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Customer>>>(
                    endpoint: "Product/CustomerShow"
                );
            if (response != null)
            {
                customname.DataSource = response.data;
                customname.DisplayMember = "Customer_Name";//指定显示的字段
                customname.ValueMember = "id";//绑定id属性
            }
        }


        /// <summary>
        /// 销售单查询
        /// </summary>
        private async void GetProductSelect6()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Sales_Ticket>>>(
                    endpoint: "Product/SalesShow"
                );
            if (response != null)
            {
                salesname.DataSource = response.data;
                salesname.DisplayMember = "Sales_Number";//指定显示的字段
                salesname.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 检验单查询
        /// </summary>
        private async void GetProductSelect7()
        {
            var response = await _apiClient.GetAsync<ApiResult<List<Goods_Inspection>>>(
                    endpoint: "Product/InspectionShow"
                );
            if (response != null)
            {
                Inspectionname.DataSource = response.data;
                Inspectionname.DisplayMember = "InspectionName";//指定显示的字段
                Inspectionname.ValueMember = "id";//绑定id属性
            }
        }

        /// <summary>
        /// 出货检验单名称/编号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Inspectionname_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedProduct = Inspectionname.SelectedItem as Goods_Inspection;
            if (selectedProduct != null)
            {
                Inspectionnumber.Text = selectedProduct.InspectionNode ?? "";

            }
            else
            {
                Inspectionnumber.Text = "";
            }
        }

        /// <summary>
        /// 新增按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void skinButton1_Click(object sender, EventArgs e)
        {
            // 检查_apiClient是否初始化
            if (_apiClient == null)
            {
                MessageBox.Show("API客户端未初始化", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // 构建请求实体对象，将窗体控件值赋值给实体属性
            var requestModel = new Stockout_Model
            {
                Stockout_Number = txtDiscount_Number.Text.Trim(),
                Stockout_Name = skinoutname.Text.Trim(),
                InspectionId = int.TryParse(Inspectionname.SelectedValue.ToString(), out int Product) ? Product : 0,
                Stockout_Time = date_creaTime1.Value,
                Customer_Id = int.TryParse(customname.SelectedValue.ToString(), out int Products) ? Products : 0,
                Sales_Id = int.TryParse(salesname.SelectedValue.ToString(), out int Productd) ? Productd : 0,
                Stockout_Desc = skindesc.Text.Trim(),
                Product_Id = int.TryParse(productname.SelectedValue.ToString(), out int Product1) ? Product1 : 0,
                Stockout_Unit = skin_until.Text.Trim(),
                CreateTime = datecreate.Value,
                OpenTime = dateopen.Value,
                Batch = skinBatch.Text.Trim(),
                Stockout_Num = int.TryParse(skinnumber.Text.Trim(), out int quantity) ? quantity : 0,
                Warehouse_Id = int.TryParse(cang1.SelectedValue.ToString(), out int Product3) ? Product3 : 0,
                AreaId = int.TryParse(cang2.SelectedValue.ToString(), out int Product4) ? Product4 : 0,
                StorageId = int.TryParse(cang3.SelectedValue.ToString(), out int Product5) ? Product5 : 0,
                Stockout_State = int.TryParse(skinpeople.Text.Trim(), out int quantitys) ? quantitys : 0,

            };
            var response = await _apiClient.PostAsync<Stockout_Model, ApiResult<string>>(
            endpoint: "Product/AddStockout",
            data: requestModel
            );
            if (response.code == 200)
            {
                // 这里可根据后端返回的结果做相应处理，比如简单提示成功
                MessageBox.Show("产品入库添加成功，后端返回：", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // 可选择清空窗体控件内容，准备下次添加

            }
            else
            {
                MessageBox.Show("调用接口失败，状态码：", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }




        }


    }
}
